package com.bird.file.serializablestream;

import java.io.*;

/**
 * @author sijunqiang
 * @data 2021/8/1 15:35
 * @description: 序列化流 写出对象到文件中
 */
public class ObjectOutputStream {


    //创建序列化流并将对象写入的到文件中
    public static void main1(String[] args) throws IOException {
        Employee employee = new Employee();
        employee.setName("熊大");
        employee.setAddress("上海");
        employee.setAge("14");
        // 创建序列化流对象
        java.io.ObjectOutputStream oop = new java.io.ObjectOutputStream(new FileOutputStream("object.txt"));
        oop.writeObject(employee);
        oop.flush();
        oop.close();
        System.out.println(employee);
    }

    //创建反序列化流 将文件中的数据写入到内存中
    public static void main(String[] args) throws IOException, ClassNotFoundException {

        // 创建反序列化流 将文件中的数据加载内存中
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.txt"));
        Employee o = (Employee) ois.readObject();
        ois.close();
        System.out.println(o);

    }


}
